<?php
/**
 * Created by PhpStorm.
 * User: 86151 - 姜伟
 * Date: 2022/8/31 19:29.
 */

namespace Xinyu\Gen\Core\Libs;

use Xinyu\Gen\Core\Config\CommonConfig;
use Illuminate\Support\Str;

class RemoveClient extends BaseClient
{
    use SingletonUtil;

    // 删除model
    public function removeModel($params = [])
    {
        $savePath = $params['save_path'] ?? '';
        $type = 'Model';
        $savePath = app_path("/Models/{$savePath}{$type}.php");
        return $this->removeFile($type, $params, $savePath);
    }

    // 删除validator
    public function removeValidator($params = [])
    {
        $savePath = $params['save_path'] ?? '';
        $type = 'Validator';
        $savePath = app_path("/Validator/{$savePath}{$type}.php");
        return $this->removeFile($type, $params, $savePath);
    }

    // 删除service
    public function removeService($params = [])
    {
        $savePath = $params['save_path'] ?? '';
        $type = 'Service';
        $module = Str::studly($params['module'] ?? CommonConfig::MODULE_ADMIN);
        $savePath = app_path("/Service/{$module}/{$savePath}{$type}.php");
        return $this->removeFile($type, $params, $savePath);
    }

    // 删除controller
    public function removeController($params = [])
    {
        $savePath = $params['save_path'] ?? '';
        $type = 'Controller';
        $module = Str::studly($params['module'] ?? CommonConfig::MODULE_ADMIN);
        $savePath = app_path("/Http/Controllers/{$module}/{$savePath}{$type}.php");
        return $this->removeFile($type, $params, $savePath);
    }

    // 判断模块控制器是否存在
    public function checkModuleControllerIsExist($name = '', $module = '')
    {
        return file_exists(app_path("/Http/Controllers/{$module}/{$name}Controller.php"));
    }

    // 删除文件
    private function removeFile($type = 'Controller', $params = [], $savePath = '')
    {
        if (empty($params) || empty($savePath)) {
            return [];
        }
        $name = $params['name'] ?? '';
        if (empty($name)) {
            return $this->error('name不能为空');
        }
        $name = Str::studly($name);
        if (!file_exists($savePath)) {
            return $this->error("{$savePath}文件不存在");
        }
        $ret = unlink($savePath);
        if (!$ret) {
            return $this->error('删除失败');
        }
        $this->removeDir($savePath);
        $module = Str::studly($params['module'] ?? CommonConfig::MODULE_ADMIN);
        return $this->success("{$module}/{$name}{$type}.php删除成功");
    }

    // 删除文件夹
    private function removeDir($savePath = '')
    {
        if (empty($savePath)) {
            return $this->error('文件路径不存在');
        }
        $list = explode('/', $savePath);
        unset($list[count($list) - 1]);
        $dir = implode('/', $list);
        $this->execRemoveDir($dir);
    }

    // 执行删除文件夹(文件夹下无文件或文件夹,则递归删除)
    private function execRemoveDir($dir = '')
    {
        if (!is_dir($dir)) {
            return false;
        }
        $fileList = scandir($dir);
        $fileLen = count($fileList) - 2;
        if ($fileLen > 0) {
            return false;
        }
        // 文件夹下不不存在文件,则删除文件夹
        rmdir($dir);
        $parentDir = $this->getParentDir($dir);
        $this->execRemoveDir($parentDir);
    }

    // 获取父级文件夹
    private function getParentDir($dir = '')
    {
        $list = explode('/', $dir);
        unset($list[count($list) - 1]);
        return implode('/', $list);
    }
}
